set more off

clear matrix
clear mata
clear
set mem 10000m
set maxvar 32767
set matsize 11000
set seed 110110111
capture log close


************** OVerview*****************
*DES: 11/23/18
* After much discussion between the 3 of us we decided to
* go back to the original AEJ:P submission data, merging in some
* of the additional variables involving test week/absences/ and 
* wind intensity leveraging the feeder routes

*We decided to move forward with this for our next submission
***********************************************



global home "C:\Florida projects\dissertation\schools wind pollution"
global output "C:\Users\Dropbox\Research on Florida Wind Patters\School wind pollution\output\whole sample results"
global samples "$home\samples"



log using "$output/Reviewer3_$S_DATE.log", replace

use "$samples\redceclptestwk_32219.dta", clear // BASED ON setuprdce_origtestweek_32219.do


merge m:1 realid using "F:\Florida projects\dissertation\school grades pollution\gender.dta"
drop if _merge==2
drop _merge


replace stability = stability/100 if stability>1
replace size=size/100


g schgrade=5 if schoolgrade=="A"
replace schgrade=4 if schoolgrade=="B"
replace schgrade=3 if schoolgrade=="C"
replace schgrade=2 if schoolgrade=="D"
replace schgrade=1 if schoolgrade=="F"


g parallelmore60=0 if placebo_any_mjrhwy!=.
replace parallelmore60=1 if placebo_any_mjrhwy>0.6 & placebo_any_mjrhwy!=.

replace behaveincident = 1 if nincident >0 & nincident!=.


drop  if strpos(schnam, "VIRTUAL")  | strpos(schnam, "ONLINE") | strpos(schnam, "ADULT") | strpos(schnam, "AMIKIDS") ///
 | strpos(schnam, "AMI KIDS") | strpos(schnam, "JAIL") | strpos(schnam, "JUVENILE") | strpos(schnam, "TRANSITION") ///
 | strpos(schnam, "HALFWAY") | strpos(schnam, "LIFE SKILLS") | strpos(schnam, "DROPOUT") | strpos(schnam, "HOSPITAL") ///
  | strpos(schnam, "PARENT")  | strpos(schnam, "DETENTION") | strpos(schnam, "SUMMER") | strpos(schnam, "ADDICTIONS") ///
  | strpos(schnam, "PACE") | strpos(schnam, "SEAL CENTER")  | strpos(schnam, "DROP BACK IN")
 
 /*maybe*/
 drop if strpos(schnam, "BLIND") | strpos(schnam, "DEAF") | strpos(schnam, "SPECIAL NEEDS")
 drop if strpos(schnam, "CITRUS HEALTH") | strpos(schnam, "HIGHLAND PARK SIPP") | strpos(schnam, "GULF/LAKE ACADEMY") ///
 | strpos(schnam, "THE PORT ACADEMY") | strpos(schnam, "E SEAL") 
 drop if zip==32084  // this is the zip code with all the special ed schools


*claudia/Jenni method variables;
g gradechange6= down60_any_mjrhwy_4*gradedum4 
g gradechange9=down60_any_mjrhwy_4*gradedum7 
g gradechange6or9=gradechange6
replace gradechange6or9=1 if gradechange9==1

*version with downwind intensity

g intensityg6 = dwintensity4h*gradedum4 
g intensityg9 = dwintensity4h*gradedum7
g intens6or9 = intensityg6
replace intens6or9= intensityg9

save "$samples\redceclptestwk_32519.dta", replace

use "$samples\redceclptestwk_32519.dta", clear


global model1 mover distmj* rdcnt_win10_mjrhwy


global model2 frl TeachDegree size stability mover distmj* rdcnt_win10_mjrhwy


global model3 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover distmj1 distmj2 distmj3 rdcnt_win10_mjrhwy


*
 ************************************************************
*Table 1: Descriptive Statistics
*************************************************************

sum  size TeachDegree frl stability momblackbyschool percenthispanic momedbyschool mommarriedby if mi_to_nid1_mjrhwy<=0.4 ///
& avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=.

unique id if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=.

preserve

keep if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=. &zip!=.

keep id changein6or9id year mi_to_nid1_mjrhwy avgfcat TeachDegree size stability mover rdcnt_win10_mjrhwy grade1 distmj1 distmj2 ///
 distmj3 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl down60_any_mjrhwy_4 zip

cap rm "`saveDIR'\Table1col2_$S_DATE.txt"
cap rm "`saveDIR'\Table1col2_$S_DATE.xml"

outreg2 using "$output\Table1col2_$S_DATE.xml", replace sum(detail) keep(size TeachDegree frl stability momblackbyschool percenthispanic momedbyschool mommarriedby) ///
 eqkeep(N mean sd)
 

drop if down60_any_mjrhwy_4==.


drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)


reghdfe avgfcat down60_any_mjrhwy_4 ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
gen sample=e(sample)==1
	
unique id if mi_to_nid1_mjrhwy<=0.4 & sample==1 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=. & zip!=.


sort id year
*generating the to downwind variables
by id: g movedown=down60_any_mjrhwy_4[_n]==0 & down60_any_mjrhwy_4[_n+1]==1 if down60_any_mjrhwy_4~=. & grade1~=.
egen todownwind=max(movedown) if down60_any_mjrhwy_4~=. & grade1~=. , by(id) 

tab movedown todownwind, missing


*generating the to upwind variables
by id: g moveup=down60_any_mjrhwy_4[_n]==1 & down60_any_mjrhwy_4[_n+1]==0 if down60_any_mjrhwy_4~=. & grade1~=.
egen toupwind=max(moveup) if down60_any_mjrhwy_4~=. & grade1~=., by(id) 


*move upwind and downwind;

gen upanddown = toupwind==1 & todownwind==1 if down60_any_mjrhwy_4~=. & grade1~=.

***differences by upwind/downwind

unique id if toupwind==1 | todownwind==1

*this is current column 3 of table 1 - just kids who change to different downwind status schools
sum size TeachDegree frl stability momblackbyschool percenthispanic momedbyschool mommarriedbyschool if toupwind==1 | todownwind==1 ///
& mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=.

unique id if (toupwind==1 | todownwind==1) & mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & down60_any_mjrhwy_4~=.

*These are all kids without missing data 
sum size TeachDegree frl stability momblackbyschool percenthispanic momedbyschool mommarriedbyschool if mi_to_nid1_mjrhwy<=0.4 ///
& avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=.

unique id if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=.

cap rm "`saveDIR'\Table1col3all_$S_DATE.txt"
cap rm "`saveDIR'\Table1col3all_$S_DATE.xml"

outreg2 using "$output\Table1col3all_$S_DATE.xml" if sample==1, replace sum(detail) keep(size TeachDegree frl stability momblackbyschool percenthispanic momedbyschool mommarriedby) ///
 eqkeep(N mean sd)


	restore

log close	
	





*
*************************************************************
*Table 2: core results 
*************************************************************

***clustered on zip code
eststo clear 
*
preserve 
* Jenni Method
drop if changein6or9id==0



	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
	


	*
foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
		forvalues j=1(1)3{

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model`j'} , absorb(zip year grade1 id) vce(cluster zip)  
	estadd scalar mean_outcome=`m'

}
}

  esttab using "$output/Table2clusterzipgr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

keep if mi_to_nid1_mjrhwy<=0.4 & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=. &zip!=.

*clustered on id and zip code
foreach var in down60_any_mjrhwy_4   { //dwintensity4h 
	foreach i in avgfcat behaveincident rabsent graderep {  //
		forvalues j=1(1)3{

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model`j'} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}
}

  esttab using "$output/Table2clusteridzipgrsamen_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}


*Row 1: Drop only abs/susp if missing abs/susp
replace rabsent=. if behaveincident==. 
replace behaveincident=. if rabsent==. 

foreach var in down60_any_mjrhwy_4    { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
		

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

	}

 esttab using "$output/Table2clusteridzipgr_noabssuspmissing_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes, keep abs/susp N the same") keep(`var') scalars(mean_outcome)
}	

*Row 2: Drop anybody missing abs/susp
drop if (behaveincident==. | rabsent==.)&year!=2001&year!=2012

eststo clear 
foreach var in down60_any_mjrhwy_4    { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
		

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

 esttab using "$output/Table2clusteridzipgr_noabssuspmissing_`var'_$S_DATE.rtf", ///
	compress append b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes, drop all if if missing abs/susp") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

***Keeping the same N across sample: 

drop if avgfcat==. | behaveincident==. | rabsent==.

*clustered on id and zip code
foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
		

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

 esttab using "$output/Table2clusteridzipgr_nomissing_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

 


restore 


*create FEs by hand;


qui tab zip if avgfcat~=. & grade1~=. & size~=. & zip~=., gen(zz) 
qui tab year if avgfcat~=. & grade1~=. & size~=. & zip~=., gen(yy)
qui tab grade1 if  avgfcat~=. & grade1~=. & size~=. & zip~=., gen(gg)


sort id year
xtset id year



***************create code (similar to event study) to divide up moves by up
* and downwind for middle and high schoolers


* ************************next try seperating upwind and downwind moves as in event study;

***********Identifying people who went downwind in 6th grade, went upwind in 6th grade, or stayed the same

local downwindvariable down60_any_mjrhwy_4 
*to downwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==0&`downwindvariable'==1&grade1==6&L.grade1==5
	egen todownwind=max(temp) if `downwindvariable'~=., by(id)  /*DES: without conditional on not missing then if you went to
	school outside of 0.4 miles but otherwise moved downwind: then you get a 1 to this value and are included in observations you are outside of 0.4 miles in the
	event study */
	label var todownwind "Moved from upwind to downwind school in 6th grade"
	drop temp
	gen group=.
	replace group=4 if todownwind==1

*to upwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==1&`downwindvariable'==0&grade1==6&L.grade1==5
	egen toupwind=max(temp) if `downwindvariable'~=., by(id) 
	label var toupwind "Moved from downwind to upwind school in 6th grade"
	drop temp
	replace group=3 if toupwind==1
	
*always upwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==0&`downwindvariable'==0&grade1==6&L.grade1==5
	egen allupwind=max(temp) if `downwindvariable'~=., by(id) 
	label var allupwind "In upwind school from 5th to 6th grade"
	drop temp
	replace group=1 if allupwind==1
	sum `downwindvariable' if allupwind==1
	
*always downwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==1&`downwindvariable'==1&grade1==6&L.grade1==5
	egen alldownwind=max(temp) if `downwindvariable'~=., by(id) 
	label var alldownwind "In doiwnwind school from 5th to 6th grade"
	drop temp
	replace group=2 if alldownwind==1
	sum `downwindvariable' if alldownwind==1
	
	label define definition 4 "To downwind" 3 "To upwind" 1 "Always upwind" 2 "Always downwind"
	label values group definition
	
	tab group `downwindvariable', missing
	
	sum D.`downwindvariable' if group==1 & grade1==6 & L.grade1==5
	sum D.`downwindvariable' if group==2 & grade1==6 & L.grade1==5
	sum D.`downwindvariable' if group==3 & grade1==6 & L.grade1==5
	sum D.`downwindvariable' if group==4 & grade1==6 & L.grade1==5
	
	
	*flag people who move downwind to upwind from 6th to 8th grade;
	
	gen dw8thflg = (L.`downwindvariable'==0 & `downwindvariable'==1) if grade1==8
	
	replace dw8thflg = 0 if dw8thflg==.
	
	tab dw8thflg, missing
	
	
	*flag people who move upwind to downwind from 6th to 8th grade;

gen up8thflg = (L.`downwindvariable'==1 & `downwindvariable'==0 ) if grade1==8

replace up8thflg=0 if up8thflg==.

tab up8thflg, missing



***Identifying people who went downwind in 9th grade, went upwind in 9th grade, or stayed the same
*to downwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==0&`downwindvariable'==1&grade1==9&L.grade1==8
	egen htodownwind=max(temp) if `downwindvariable'~=., by(id) 
	label var htodownwind "Moved from upwind to downwind school in 9th grade"
	drop temp
	gen hgroup=.
	replace hgroup=4 if htodownwind==1
	
	
*to upwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==1&`downwindvariable'==0&grade1==9&L.grade1==8
	egen htoupwind=max(temp) if `downwindvariable'~=., by(id) 
	label var htoupwind "Moved from downwind to upwind school in 9th grade"
	drop temp
	replace hgroup=3 if htoupwind==1
	

	
*always upwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==0&`downwindvariable'==0&grade1==9&L.grade1==8
	egen hallupwind=max(temp) if `downwindvariable'~=., by(id) 
	label var hallupwind "In upwind school from 8th to 9th grade"
	drop temp
	replace hgroup=1 if hallupwind==1
	sum `downwindvariable' if hallupwind==1
	

	
*always downwind
	gen temp=0
	replace temp=1 if L.`downwindvariable'==1&`downwindvariable'==1&grade1==9&L.grade1==8
	egen halldownwind=max(temp) if `downwindvariable'~=., by(id) 
	label var halldownwind "In doiwnwind school from 8th to 9th grade"
	drop temp
	replace hgroup=2 if halldownwind==1
	sum `downwindvariable' if halldownwind==1
	

			
	
	label define hdefinition 4 "To downwind" 3 "To upwind" 1 "Always upwind" 2 "Always downwind"
	label values hgroup hdefinition
	

		
	sum D.`downwindvariable' if hgroup==1 & grade1==9 & L.grade1==8
	sum D.`downwindvariable' if hgroup==2 & grade1==9 & L.grade1==8
	sum D.`downwindvariable' if hgroup==3 & grade1==9 & L.grade1==8
	sum D.`downwindvariable' if hgroup==4 & grade1==9 & L.grade1==8


tab group, missing
tab hgroup, missing




preserve

************************************************************************
* only include rising fifth graders/ 6th graders.  drop repeat grades;  
*also note, this means in this simple first difference the grade dummies (6th-5th = 0 always) drop out; 
*when included all coefficients are arbitrarily close to zero, so drop;
**************************************************************************


***** Now pull Middle / High but seperate by up/downwind;


* just downwind;


drop if  (grade1==6 & L.grade1~=5) /*excludes repeating sixth grade */
drop if (grade1==5 & F.grade1~=6)  /*excludes those who repeat fifth grade */


drop if  (grade1==9 & L.grade1~=8) /*excludes repeating sixth grade */
drop if (grade1==8 & F.grade1~=9)  /*excludes those who repeat fifth grade */


* can't be missing both;

drop if hgroup==. & group==.

tab group hgroup, missing


sum down60_any_mjrhwy_4 if avgfcat~=. & grade1~=. & size~=. & zip~=. & (hgroup~=. | group ~=.) 


sum down60_any_mjrhwy_4 if avgfcat~=. & grade1~=. & size~=. & zip~=. & (hgroup~=3 & group ~=3) 

sum down60_any_mjrhwy_4 if avgfcat~=. & grade1~=. & size~=. & zip~=. & (hgroup~=4 & group ~=4) 




sum down60_any_mjrhwy_4 if avgfcat~=. & grade1~=. & size~=. & zip~=. & (hgroup~=3 & group ~=3) 


foreach var in down60_any_mjrhwy_4  { 

		tab down60_any_mjrhwy_4 if (group~=4 & hgroup~=4) & dw8thflg==0

	foreach i in avgfcat behaveincident rabsent {  //
	


	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=. 
	local m = r(mean)

* no FE, only differences
eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy* if (group~=3 & hgroup~=3), noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'



* now exclude "off" grade moves;

eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy* if (group~=3 & hgroup~=3) & dw8thflg==0, noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/FDDWallgr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(D.`var') scalars(mean_outcome)
	
eststo clear 
}







sum down60_any_mjrhwy_4 if avgfcat~=. & grade1~=. & size~=. & zip~=. & (hgroup~=4 & group ~=4) 

gen upwindmove = down60_any_mjrhwy_4*-1


foreach var in upwindmove { 

	tab upwindmove if (group~=4 & hgroup~=4) & up8thflg==0

	foreach i in avgfcat behaveincident rabsent {  //

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=. 
	local m = r(mean)
	

* no FE, only differences
eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy* if (group~=4 & hgroup~=4), noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

* no FE, only differences
eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy* if (group~=4 & hgroup~=4) & up8thflg==0, noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'



}

  esttab using "$output/FDUWallgr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(D.`var') scalars(mean_outcome)
	
eststo clear 
}



*****Finally combine all moves (up and downwind) together;


foreach var in down60_any_mjrhwy_4  { 
	foreach i in avgfcat behaveincident rabsent {  //

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=. 
	local m = r(mean)

* no FE, only differences
eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy*, noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'



* now exclude "off" grade moves;

eststo: reghdfe D.`i' D.`var'  D.momedbyschool D.momblackbyschool D.mommarriedbyschool D.percenthispanic D.frl D.TeachDegree D.size D.stability D.mover D.distmj1 D.distmj2 D.distmj3 D.rdcnt_win10_mjrhwy D.zz*  D.yy* if  dw8thflg==0 & up8thflg==0, noabsorb vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/FDAllallgr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(D.`var') scalars(mean_outcome)
	
eststo clear 
}







log close

stop;






*************************************************************
*Table 3 Week of Test
*************************************************************


set more off

eststo clear 




*
* Jenni Method
preserve

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)





	foreach i in avgfcat behaveincident rabsent { 
	
	
eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3}  if down60_any_mjrhwy_4_test~=. & down60_any_mjrhwy_4~=. , absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' down60_any_mjrhwy_4_test ${model3}  if down60_any_mjrhwy_4_test~=. & down60_any_mjrhwy_4~=. , absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' down60_any_mjrhwy_4_test down60_any_mjrhwy_4 ${model3}  if down60_any_mjrhwy_4_test~=. & down60_any_mjrhwy_4~=. , absorb(zip year grade1 id) vce(cluster id zip)  

 esttab using "$output\Table3_weekdown60gr_`i'$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(+ .1 * .05 ** .01 *** .001) ///
	title ("effect of `var' on outcomes") keep(down60_any_mjrhwy_4 down60_any_mjrhwy_4_test) 

eststo clear

}




	foreach i in avgfcat behaveincident rabsent { 
	
	
eststo: reghdfe `i' dwintensity4h ${model3}  if dwintensity4h_test~=. & dwintensity4h~=., absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' dwintensity4h_test ${model3} if dwintensity4h_test~=. & dwintensity4h~=. , absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' dwintensity4h_test dwintensity4h ${model3} if dwintensity4h_test~=.  & dwintensity4h~=., absorb(zip year grade1 id) vce(cluster id zip)  

 esttab using "$output\Table3_weekintensitygr_`i'$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(+ .1 * .05 ** .01 *** .001) ///
	title ("effect of `var' on outcomes") keep(dwintensity4h_test dwintensity4h) 
eststo clear
 
}

*
************************************************************
*Table 4: Demographic Subgroups
*************************************************************
*Heterogeneity by race

*preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

estimates clear

foreach j in white black hisp {  
foreach i in  avgfcat behaveincident rabsent  {  

*eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if `j'==1, absorb(zip grade1 year id) vce(cluster id zip)
	
eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if `j'==1, absorb(zip grade1 year id) resid(p`j'`i') //vce(cluster id zip)
estimates store est`j'`i'
predict `j'`i', score
}
      esttab using "$output\Table4raceeth`j'y$S_DATE.rtf", ///
	scalar(N_g) sfmt(0) compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4) mtitles("avgfcat" "behav incident" "absences" ) 
eststo clear 
	
	}	

suest estwhiteavgfcat estblackavgfcat esthispavgfcat, vce(cluster zip)
test [estwhiteavgfcat]down60 = [estblackavgfcat]down60 = [esthispavgfcat]down60

suest estwhitebehaveincident estblackbehaveincident esthispbehaveincident, vce(cluster zip)
test [estwhitebehaveincident]down60 = [estblackbehaveincident]down60 = [esthispbehaveincident]down60

suest estwhiterabsent estblackrabsent esthisprabsent, vce(cluster zip)
test [estwhiterabsent]down60 = [estblackrabsent]down60 = [esthisprabsent]down60


estimates clear

*Heterogeneity by FRL - stratifying

egen anyfrl = max(frl), by(id)
egen allfrl = min(frl), by(id)
gen somefrl = (anyfrl == 1 & allfrl == 0)
gen nofrl = (anyfrl == 0)

foreach j in allfrl somefrl nofrl { // 
foreach i in  avgfcat behaveincident rabsent {  

*eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if `j'==1, absorb(zip grade1 year id) vce(cluster id zip)
	
eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if `j'==1, absorb(zip grade1 year id) resid(l`j'`i') //vce(cluster id zip)
estimates store est`j'`i'
predict `j'`i', score

}

      esttab using "$output\Table4frl`j'y_$S_DATE.rtf", ///
	scalar(N_g) sfmt(0) compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4) mtitles("avgfcat" "behav incident" "absences") 
eststo clear 
	}
	
suest estallfrlavgfcat estsomefrlavgfcat estnofrlavgfcat, vce(cluster zip)
test [estallfrlavgfcat]down60 = [estsomefrlavgfcat]down60 = [estnofrlavgfcat]down60

suest estallfrlbehaveincident estsomefrlbehaveincident estnofrlbehaveincident, vce(cluster zip)
test [estallfrlbehaveincident]down60 = [estsomefrlbehaveincident]down60 = [estnofrlbehaveincident]down60

suest estallfrlrabsent estsomefrlrabsent estnofrlrabsent, vce(cluster zip)
test [estallfrlrabsent]down60 = [estsomefrlrabsent]down60 = [estnofrlrabsent]down60
	
estimates clear
	
*Heterogeneity by gender

if `Claudia'==1{
*rename studentid realid
sort realid

merge m:1 realid using "$samples\gender.dta", gen(_mfem)
drop if id==.
g femaleinteract=female*down60_any_mjrhwy_4
}

if `David'==1{
gen findex = runiform() if down60_any_mjrhwy_4~=.
gen female=findex>0.5 if down60_any_mjrhwy_4~=.
g femaleinteract =female*down60_any_mjrhwy_4
}

foreach i in  avgfcat behaveincident rabsent  {  
*eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if female==1, absorb(zip grade1 year id) vce(cluster id zip)

eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if female==1, absorb(zip grade1 year id) resid(f`i') //vce(cluster id zip)
estimates store estf`i'
predict fem`i', score

}
      esttab using "$output\Table4genFem_$S_DATE.rtf", ///
	scalar(N_g) sfmt(0) compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4 )  
eststo clear 

foreach i in  avgfcat behaveincident rabsent  { 
*eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if female==0, absorb(zip grade1 year id) vce(cluster id zip)
 
eststo: reghdfe `i' down60_any_mjrhwy_4 ${model3} if female==0, absorb(zip grade1 year id) resid(m`i') //vce(cluster id zip)
estimates store estm`i'
predict mal`i', score

}

      esttab using "$output\Table4genMale_$S_DATE.rtf", ///
	scalar(N_g) sfmt(0) compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4 )  
eststo clear 

suest estfavgfcat estmavgfcat, vce(cluster zip)
test [estfavgfcat]down60 = [estmavgfcat]down60

suest estfbehaveincident estmbehaveincident, vce(cluster zip)
test [estfbehaveincident]down60 = [estmbehaveincident]down60

suest estfrabsent estmrabsent, vce(cluster zip)
test [estfrabsent]down60 = [estmrabsent]down60



*
*************************************************************
*Table 5: balancing test 
*************************************************************
eststo clear
set more off
		


* Jenni Method
eststo clear
*
preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
*/	
foreach var of varlist size stability TeachDegree frl momedbyschool momblackbyschool mommarriedbyschool percenthispanic schgrade { 

local model1 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl  TeachDegree size stability mover distmj* rdcnt_win10_mjrhwy

local except "`var'"
local newlist: list model1 - except
di "`newlist'"
local control = "`newlist'"


eststo: reghdfe `var' down60_any_mjrhwy_4  `control'  if avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=., absorb(zip year grade1 id) vce(cluster id zip)  
	
	}
	
esttab using "$output\Table5jenni_baldown60gr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star( * .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4 )
	eststo clear

	
	
foreach var of varlist size stability TeachDegree frl momedbyschool momblackbyschool mommarriedbyschool percenthispanic schgrade{ 

local model1 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl  TeachDegree size stability mover distmj* rdcnt_win10_mjrhwy

local except "`var'"
local newlist: list model1 - except
di "`newlist'"
local control = "`newlist'"


eststo: reghdfe `var' dwintensity4h `control'  if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=., absorb(zip year grade1 id) vce(cluster id zip)  
	
	}
	
esttab using "$output\Table5baljenni_dwintensit4hgr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star( * .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(dwintensity4h)
	eststo clear

eststo clear
restore


* All Movers


*
foreach var of varlist size stability TeachDegree frl momedbyschool momblackbyschool mommarriedbyschool percenthispanic schgrade { 

local model1 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl  TeachDegree size stability mover distmj* rdcnt_win10_mjrhwy

local except "`var'"
local newlist: list model1 - except
di "`newlist'"
local control = "`newlist'"


eststo: reghdfe `var' down60_any_mjrhwy_4 `control' if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=., absorb(zip grade1 year id) vce(cluster id zip)  
	
	}
	
esttab using "$output\Table5_baldown60_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star( * .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(down60_any_mjrhwy_4)
	eststo clear

foreach var of varlist size stability TeachDegree frl momedbyschool momblackbyschool mommarriedbyschool percenthispanic schgrade { 

local model1 momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl  TeachDegree size stability mover distmj* rdcnt_win10_mjrhwy

local except "`var'"
local newlist: list model1 - except
di "`newlist'"
local control = "`newlist'"


eststo: reghdfe `var' dwintensity4h `control' if mi_to_nid1_mjrhwy<=0.4 & avgfcat!=. & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=., absorb(zip grade1 year id) vce(cluster id zip)  
	
	}
	
esttab using "$output\Table5bal_dwintensit4h_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star( * .1 ** .05 *** .01)  ///
	title ("effect of winddirection on `i'") keep(dwintensity4h)
	eststo clear

eststo clear
		

		
********************************************************
* Table 6: No Choice zipcodes
*******************************************************


preserve

keep if zip==34285 | zip== 33801 | zip== 33603 | zip== 33142 | zip==33136 | zip==32407 
drop if strpos(schnam, "BLIND") | strpos(schnam, "DEAF") | strpos(schnam, "SPECIAL NEEDS") | strpos(schnam, "LITTLE HAVANA")


* Jenni Method

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

xtset id year
eststo clear
foreach var in  down60_any_mjrhwy_4 dwintensity4h  {   //
	foreach i in avgfcat behaveincident rabsent { 
	
	
eststo: xtreg `i' `var' ${model3} gradedum* i.zip, fe vce(cluster id zip)  nonest

	estadd scalar mean_outcome=`m'
}

  esttab using "$output\Table6_6zipsgr`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star( * .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 

}

restore
		
		

		
*************************************************************
*Table 7: Additional Robustness and Validity Tests, 
*************************************************************


replace placebo_any_mjrhwy=. if mi_to_nid1_mjrhwy>0.4


*****
*COLUMN 2:
* drop schools close enough to be influenced by noise, dust kickups;
* note: col 1 is just a replication of core results on fcat.  
*******


* Jenni Method
preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
	
	drop if mi_to_nid1_mjrhwy<=0.03

foreach var in down60_any_mjrhwy_4  dwintensity4h  { 

	foreach i in avgfcat behaveincident rabsent  { 

	

eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
}

  esttab using "$output\Table7dropclsgr`var'__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') 
eststo clear 

}

restore

*****
*COLUMN 3:
* downwind replaced with parallel wind
*******


* Jenni Method
preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

	foreach i in avgfcat behaveincident rabsent   { 

	
eststo: reghdfe `i' parallelmore60 ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  



      esttab using "$output\Table7parllelgr_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of parallel on outcomes") keep(parallelmore60)  
}
eststo clear 
restore 

*****
*COLUMN 4:
* drop movers
*******

	*remove those observed in a different zipcodes between 5th and 10th,

* identify zipcode in grade 5

eststo clear 

preserve

gen zip5imp = zip if grade1==5
sort id zip
by id: egen zip5th = max(zip5imp) if zip~=.

drop zip5imp

*create average zipcode
gen zip5to10 = zip if (grade1>=5 & grade1<=10) & grade1~=.
by id: egen mezip = mean(zip5to10) if zip~=.



* Jenni Method
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

drop if mezip~=zip5th

foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in  avgfcat behaveincident rabsent {  

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

  esttab using "$output/Table7nomovegr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

restore



*****
*COLUMN 5:
* control for average school FCAT
*******


eststo clear 

* Jenni Method
*preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

merge m:1 school year using "C:\Florida projects\dissertation\avgfcatbyschool", gen(_mabs)
drop if id==.


foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in  avgfcat behaveincident rabsent  {  

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} avgfcatbyschool2 , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/Table7_avgfcatbyschoolgr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

restore

*************************************************************
* table 8: Pollution
*************************************************************
* see "first stage" folder

*************************************************************
*Appendix table A1: All movers
*************************************************************

*All kids
foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in  avgfcat behaveincident rabsent {  
		

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}
}

  esttab using "$output/TableA1allkidsgr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(down60_any_mjrhwy_4  dwintensity4h ) scalars(mean_outcome)
	
eststo clear 

*************************************************************
*Appendix table A2: sensitivity to controls
*************************************************************

* results produced in table 2 output coded above


*/ 
*************************************************************
*Appendix Table A1  whole sample results
*************************************************************
*clustered on id and zip code
foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in avgfcat behaveincident rabsent graderep {  //

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/TableA1WholeSampleclusteridzipgr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}
*
*************************************************************
*Appendix Table A3 Placebo
*************************************************************


gen rand = runiform()
gen randdownwind= .
replace randdownwind = 0 if rand <0.6
replace randdownwind = 1 if rand >=0.6
replace randdownwind=. if mi_to_nid1_mjrhwy>0.4

gen randintense = rand
replace randintense = . if mi_to_nid1_mjrhwy>0.4

set more off

preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)


foreach var in randdownwind randintense  {  
	foreach i in avgfcat behaveincident rabsent  { 
	

* Jenni Method


eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
 
}
  esttab using "$output\Table5placebo60y_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(+ .1 * .05 ** .01 *** .001) ///
	title ("effect of `var' on outcomes") keep(`var')  
eststo clear 

}

restore




*************************************************************
*Appendix Table A4  only treated zipcodes
*************************************************************


*one concern voiced by the referees is that we should compare the same number
* of upwind and downwind schools;

* we can't really do that, but we can drop all zipcodes without at least one downwind 

preserve

*count by zipcode the number of downwind observations

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

	
* count the number of upwind downwind schools

	egen down60_by_zip=total(down60_any_mjrhwy_4), by(zip)
tab down60_by_zip, missing

drop if down60_by_zip ==0





*only keep zipcodes with at least one downwind observation, then count the number of upwind/downwind schools
egen upnumzip = tag(school) if down60_any_mjrhwy_4==0
tab upnumzip, missing

egen dwnnumzip = tag(school) if down60_any_mjrhwy_4==1
tab dwnnumzip



eststo clear 

* Jenni Method


foreach var in down60_any_mjrhwy_4  dwintensity4h  { 
	foreach i in  avgfcat behaveincident rabsent {  

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3}, absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/TableSrtDWrgr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

restore


*


************
*week of test effects by % time downwind


preserve


drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
*	
	foreach i in avgfcat behaveincident rabsent { 
	
	
eststo: reghdfe `i' dwind_any_win4_mjrhwy ${model3} if dwind_any_win4_test~=. & dwind_any_win4_mjrhwy~=. , absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' dwind_any_win4_test ${model3} if dwind_any_win4_test~=. & dwind_any_win4_mjrhwy~=. , absorb(zip year grade1 id) vce(cluster id zip)  

eststo: reghdfe `i' dwind_any_win4_mjrhwy dwind_any_win4_test ${model3} if dwind_any_win4_test~=. & dwind_any_win4_mjrhwy~=. , absorb(zip year grade1 id) vce(cluster id zip)  

 esttab using "$output\Jennitestweek_dwindgr_`i'$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(+ .1 * .05 ** .01 *** .001) ///
	title ("effect of `var' on outcomes") keep(dwind_any_win4_mjrhwy dwind_any_win4_test) 
eststo clear
 
}
*/
************************Controlling for absences

eststo: reghdfe avgfcat down60_any_mjrhwy_4 rabsent ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
eststo: reghdfe avgfcat dwintensity4h rabsent ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  

esttab using "$output\ControlabsencesJennigr_`i'$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(+ .1 * .05 ** .01 *** .001) ///
	title ("effect of `var' on outcomes") keep(down60_any_mjrhwy_4 dwintensity4h) 
eststo clear

restore



********************Testing to see if summer wind predicts test scores

use "$samples\redceclptestwk_112118.dta", clear

*
merge m:1 school using "$roadsetup\remake\collapsed_5closest_mjrsummer_2010.dta", gen(_msum)
drop if id==.

gen down60_any_mjrhwy_4_sum=(dwind_any_win4_sum>=.6) 
replace down60_any_mjrhwy_4_sum=. if mi_to_nid1_mjrhwy>.4 | dwind_any_win4_sum==.

rename windtreat1_sum dwintensity4h_sum

corr down60_any_mjrhwy_4_sum down60_any_mjrhwy_4

preserve

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

foreach i in avgfcat behaveincident rabsent  {  //graderep
	reghdfe `i' down60_any_mjrhwy_4 ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	gen sample_`i'=e(sample)==1
}

corr down60_any_mjrhwy_4_sum down60_any_mjrhwy_4


foreach var in down60_any_mjrhwy_4_sum    { 
	foreach i in avgfcat behaveincident rabsent  {  //graderep
		
	sum `i' if down60_any_mjrhwy_4_sum~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' down60_any_mjrhwy_4_sum down60_any_mjrhwy_4 ${model3} if sample_`i'==1, absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'


}

  esttab using "$output/summerwindgrboth_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(down60*) scalars(mean_outcome)
	
eststo clear 
}

restore

*first replicate baseline results and define isample;


* Jenni Method

preserve
drop if changein6or9id==0



	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
	




* first a reality check: make sure we can replicate core results on avgfcat;

	

keep if mi_to_nid1_mjrhwy<=0.4 & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=. &zip!=.

*clustered on id and zip code
foreach var in down60_any_mjrhwy_4   { //dwintensity4h 
	foreach i in avgfcat behaveincident rabsent {  //

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'
	gen sample_`i'=e(sample)==1

}


eststo clear 
}



**********************Two way fixed effects

* Jenni Method
preserve
drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)


g zipbygrade = grade1*1000000 + zip

g zipbyyear = year*1000000 + zip

g gradebyyear = year*10000 + grade1

if `Claudia'==1 {
g school1=school
tostring(school1), force replace
g districta=substr(school1,1,2) if school>100000
replace districta=substr(school1,1,1) if school<100000
destring districta, force replace 
g district = DistrictNumber
replace district=districta if district==.
}

g districtbygrade = grade1*1000000 + DistrictNumber

g districtbyyear = year*1000000 + DistrictNumber


sum grade1 zip year id districtbygrade districtbyyear gradebyyear zipbyyear zipbygrade if sample_avgfcat==1


eststo clear 


*******District by grade FEs	
set more off
foreach var in down60_any_mjrhwy_4    {  //dwintensity4h
	foreach i in avgfcat behaveincident rabsent  {  //graderep

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
	sum `i' if sample_`i'==1
	unique id if sample_`i'==1


	
eststo: reghdfe `i' `var' ${model3} if sample_`i'==1, absorb(districtbygrade zip year id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

	
	
}

  esttab using "$output/districtbygradezip_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

*
*******District by year FEs	

foreach var in down60_any_mjrhwy_4    {  //dwintensity4h
	foreach i in avgfcat behaveincident rabsent  {  //graderep

	sum `i' if sample_`i'==1
	local m = r(mean)
	
	unique id if sample_`i'==1
	
eststo: reghdfe `i' `var' ${model3} if sample_`i'==1, absorb(districtbyyear zip grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

  esttab using "$output/districtbyyear_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

*/

*******Zipcode by grade FEs	

foreach var in down60_any_mjrhwy_4    { //dwintensity4h
	foreach i in avgfcat behaveincident rabsent  {  //graderep

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
		sum `i' if sample_`i'==1
	unique id if sample_`i'==1

	
eststo: reghdfe `i' `var' ${model3} if sample_`i'==1, absorb(zipbygrade year id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

  esttab using "$output/zipbygradenoy_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}


*
*Zipcode by year FEs	

foreach var in down60_any_mjrhwy_4    { //dwintensity4h
	foreach i in avgfcat behaveincident rabsent  {  //graderep

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} if sample_`i'==1, absorb(zipbyyear grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

  esttab using "$output/zipbyyeargr_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

*/

*Grade by year FEs	

foreach var in down60_any_mjrhwy_4    { //dwintensity4h
	foreach i in avgfcat behaveincident rabsent  {  //graderep

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
		sum `i' if sample_`i'==1
	unique id if sample_`i'==1

	
eststo: reghdfe `i' `var' ${model3} if sample_`i'==1, absorb(gradebyyear zip id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}

  esttab using "$output/gradebyyear_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

restore



***Figuring out what the variation is

collapse down60_any_mjrhwy_4 zip, by(school grade1)
drop if down60_any_mjrhwy_4==.
drop if zip==.

g temp5=grade1==5
g temp6=grade1==6
g temp9=grade1==9

bysort school: egen g5=max(temp5)
by school: egen g6=max(temp6)
by school: egen g9=max(temp9)

sort zip school grade1
bysort zip: egen num5byzip=count(temp5)

*generate counts for downwind schools or upwind schools in 5th grade
by zip: egen num5byzipdw=count(temp5) if down60_any_mjrhwy_4==1 & temp5==1
by zip: egen num5byzipuw=count(temp5) if down60_any_mjrhwy_4==0 & temp5==1

*generate counts for downwind schools or upwind schools in 6th grade
by zip: egen num6byzipdw=count(temp6) if down60_any_mjrhwy_4==1 & temp6==1
by zip: egen num6byzipuw=count(temp6) if down60_any_mjrhwy_4==0 & temp6==1

*generate counts for downwind schools or upwind schools in 9th grade
by zip: egen num9byzipdw=count(temp9) if down60_any_mjrhwy_4==1 & temp9==1
by zip: egen num9byzipuw=count(temp9) if down60_any_mjrhwy_4==0 & temp9==1

*there are 13 zips with one downwind school in 5th grade, 4 zips with 2 downwind 5th grade schools, 
by zip: egen num5byzipdw1=max(num5byzipdw)
by zip: egen num6byzipdw1=max(num6byzipdw)
by zip: egen num9byzipdw1=max(num9byzipdw)

by zip: egen num5byzipuw1=max(num5byzipuw)
by zip: egen num6byzipuw1=max(num6byzipuw)
by zip: egen num9byzipuw1=max(num9byzipuw)

*variation if there's at least one downwind and one upwind school in the same grade and zip code
bysort zip: gen variation=1 if (num5byzipdw1>=1 & num5byzipuw1>=1 & num5byzipdw1!=. & num5byzipuw1!=.) | ///
(num6byzipdw1>=1 & num6byzipuw1>=1 & num6byzipdw1!=. & num6byzipuw1!=.) | ///
(num9byzipdw1>=1 & num9byzipuw1>=1 & num9byzipdw1!=. & num9byzipuw1!=.) 

unique zip if variation==1 //only 11 zip codes contribute to variation in the grade-zip specification

bysort zip: gen variationorig=1 if (num5byzipdw1>=1 & num6byzipuw1>=1 & num5byzipdw1!=. & num6byzipuw1!=.) | ///
(num6byzipdw1>=1 & num5byzipuw1>=1 & num6byzipdw1!=. & num5byzipuw1!=.) | ///
(num5byzipdw1>=1 & num9byzipuw1>=1 & num5byzipdw1!=. & num9byzipuw1!=.) | ///
(num9byzipdw1>=1 & num5byzipuw1>=1 & num9byzipdw1!=. & num5byzipuw1!=.) | ///
(num6byzipdw1>=1 & num9byzipuw1>=1 & num6byzipdw1!=. & num9byzipuw1!=.) | ///
(num9byzipdw1>=1 & num6byzipuw1>=1 & num9byzipdw1!=. & num6byzipuw1!=.) 

unique zip if variationorig==1 //only 10 zip codes contribute to variation in the grade-zip specification




restore



*************************************************************
*Reviewer 3 response (based on Table 2: core results)
*************************************************************
preserve
*
merge m:1 school using "$roadsetup\remake\collapsed_All190319_adddata.dta"
drop if id==.

*Downwind intensity for highways, trucnated:
gen dwintensity4hnew = windISnew if mi_to_nid_IS4<=0.4
replace dwintensity4hnew=windUSHnew if dwintensity4hnew==. & mi_to_nid_USH4<=0.4
	label var dwintensity4hnew "Mean of intensity hwy, 1=parallel to downwind"


*Downwind cosine, truncated:
gen cosine = cosineIS if mi_to_nid_IS4<=0.4
replace cosine=cosineUSH if cosine==. & mi_to_nid_USH4<=0.4
	label var cosine "Cosine-based estimate"	
	
***cosine bins: 

forvalues i=1/9 {
gen cosinedistmj`i' = cosine>0.`i'-0.1 & cosine<=0.`i'
}


eststo clear 

* Jenni Method
drop if changein6or9id==0
	
	*indicator for missing
	
	
	gen move_dist_imp=move_dist
	replace move_dist_imp=0 if move_dist==.
	gen move_dist_miss=(move_dist==.)
		label var move_dist_imp "Move distance, imputed for missing"
		label var move_dist_miss "Move distance missing"

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)
	
	sum move_dist
	sum move_dist if down60_any_mjrhwy_4!=.
	egen p50 = pctile( move_dist ) if mi_to_nid1_mjrhwy<=0.4, p(50) 
	egen medianmove=median(move_dist) if mi_to_nid1_mjrhwy<=0.4
	sum medianmove
	
	
	
	
	
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} 								, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist!=.				, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist						, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist_imp move_dist_miss	, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat dwintensity4hnew 	${model3} 								, absorb(zip year grade1 id) vce(cluster zip)  
	*eststo: reghdfe avgfcat cosine	 					   		momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 cosinedistmj*  	momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster zip)  
	eststo: reghdfe avgfcat dwintensity4hnew 	cosinedistmj*  	momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster zip)  

	esttab using "$output/TX_Response_Distance__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip") keep(down60_any_mjrhwy_4 move_dist dwintensity4hnew move_dist_imp move_dist_miss)  ///
	mtitle("Baseline" "Limited distance sample" "Add control for distance" "Censored intensity" "Down 60, cosine distance dummies" "Censored intensity, cosine distance dummies")
	

*clustered on id and zip code

	set more off	
	

	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist<=p50					, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist>p50					, absorb(zip year grade1 id) vce(cluster id zip)  

  esttab using "$output/TX_Response_Distancecutmovedist__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip") keep(down60_any_mjrhwy_4)  ///
	mtitle("<50th percentile move_dist" ">50th percentile move_dist" "Add control for distance" "Censored intensity" "Down 60, cosine distance dummies" "Censored intensity, cosine distance dummies")
	*/
	
	
eststo clear 

*clustered on id and zip code - with 5-6 and 8-9 pairs defining distance
	gen move_dist6=0 if grade1==6
	replace move_dist6=move_dist_imp if grade1==6
	egen move_dist56=max(move_dist6), by(id)
	
* create a flag for moving zero in 6th grade

	gen movezero6 = move_dist6==0 if grade1==6
     tab movezero6, missing

	
	gen move_dist9=0 if grade1==9
	replace move_dist9=move_dist_imp if grade1==9
	egen move_dist89=max(move_dist9), by(id)

	*create flag for moving zero in 9th grade
	
	gen movezero9 = (move_dist9==0) if grade1==9
	     tab movezero9, missing

	
gen movezero69 = 1  if movezero6==1 | movezero9==1

sum avgfcat
sum avgfcat if movezero69~=1

	
	egen move_dist5689=max(move_dist_imp) if grade1==5|grade1==6|grade1==8|grade1==9, by(id)
	

	
	
	gen move_dist_split=move_dist56 if grade1==5|grade1==6
	replace	move_dist_split=move_dist89 if grade1==8|grade1==9
	
	replace	move_dist_split=move_dist56 if move_dist_split==.
	replace	move_dist_split=move_dist89 if move_dist_split==.

	replace move_dist_split=move_dist5689 if move_dist_split==. //taking the biggest move in 5-6 OR 8-9, if move dist is still missing (could happen if we have a kid in, say 6 and 8 but missing other grades? - RARE)
	replace move_dist_split=0 if move_dist_split==. // Didn't actually move if still missing at this point...
	
	/*note, droping move_dist_split, will drop the individual, not just the observation */ 
	
	

	
	
	
	egen p50_split = pctile( move_dist_split ) if mi_to_nid1_mjrhwy<=0.4, p(50) 

	set more off	
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split<=p50_split					, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split>p50_split					, absorb(zip year grade1 id) vce(cluster id zip)  

	
  esttab using "$output/TX_Response_Distancecutmovedist_alt__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip") keep(down60_any_mjrhwy_4)  ///
	mtitle("<50th percentile move_dist" ">50th percentile move_dist" "Add control for distance" "Censored intensity" "Down 60, cosine distance dummies" "Censored intensity, cosine distance dummies")
*/


***Response to Reviewer 1, Second review (01 NOV 19):


*first, make sure we can replicate baseline results:


keep if mi_to_nid1_mjrhwy<=0.4 & TeachDegree!=. & size!=. & stability!=. & mover!=. & rdcnt_win10_mjrhwy!=. & grade1!=. & distmj1!=. & distmj2!=. ///
& distmj3!=. & momedbyschool!=. & momblackbyschool!=. & mommarriedbyschool!=. & percenthispanic!=. & frl!=. & down60_any_mjrhwy_4~=. &zip!=.



sum avgfcat if grade1==6
sum avgfcat if grade1==9
sum avgfcat if grade1==6 | grade1==9
	
sum avgfcat  if grade1==6 & movezero6==1 
sum avgfcat if grade1==9 & movezero9==1
sum avgfcat if (grade1==6 & movezero6==1) | (grade1==9 & movezero9==1)

	
sum avgfcat  if grade1==6 & (movezero6==1 & move_dist==.)
sum avgfcat if grade1==9 & (movezero9==1 & move_dist==.)
sum avgfcat if (grade1==6 & movezero6==1 & move_dist==.) | (grade1==9 & movezero9==1  & move_dist==.)





*clustered on id and zip code
foreach var in down60_any_mjrhwy_4   { 
	foreach i in avgfcat behaveincident rabsent {  
		forvalues j=3(1)3{

	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model`j'} , absorb(zip year grade1 id) vce(cluster id zip)  
	estadd scalar mean_outcome=`m'

}
}

  esttab using "$output/Table2chk`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}




*first drop amyone who ever had a zero move distance as a check (should drop those 
* who didn't move from fourth to fifth or seventh to eight.  


	egen p50_split_no0 = pctile( move_dist_split ) if mi_to_nid1_mjrhwy<=0.4&move_dist_split!=0, p(50) 
	
	sum p50_split_no0
	
	
		eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3}  									if 	move_dist_imp!=0			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist_imp move_dist_miss 		if 	move_dist_imp!=0			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split<=p50_split_no0	& move_dist_imp!=0			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split>p50_split_no0  & move_dist_imp!=0			, absorb(zip year grade1 id) vce(cluster id zip)  
  esttab using "$output/TX_Response_Distancecutmovedist_NoZero__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip, removing zeros") keep(down60_any_mjrhwy_4 move_dist_imp move_dist_miss)  ///
	mtitle("Baseline without 0 dist" "Adding control for move dist" "<50th percentile move_dist" ">50th percentile move_dist" )

	eststo clear

	
	
*Next drop observations who were zero for moving in 6th 9th 	

		eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3}  								if 	(movezero69~=1) 			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist_imp move_dist_miss 		if 	(movezero69~=1)			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split<=p50_split_no0	& (movezero69~=1)		, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split>p50_split_no0  & (movezero69~=1)	, absorb(zip year grade1 id) vce(cluster id zip)  
  esttab using "$output/TX_Response_Distancecutmovedist_69move__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip, removing zeros") keep(down60_any_mjrhwy_4 move_dist_imp move_dist_miss)  ///
	mtitle("Baseline without 0 dist" "Adding control for move dist" "<50th percentile move_dist" ">50th percentile move_dist" )

	eststo clear

	
*finally, drop all individuals if zero only if the move from 5to6 or 8 to 9 is zero

	
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3}  									if move_dist_split!=0		, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist_imp move_dist_miss 		if 	move_dist_split!=0		, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split<=p50_split_no0	& move_dist_split!=0	, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist_split>p50_split_no0  & move_dist_split!=0	, absorb(zip year grade1 id) vce(cluster id zip)  
  esttab using "$output/TX_Response_Distancecutmovedist_dropindvmv__$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip, removing zeros") keep(down60_any_mjrhwy_4 move_dist_imp move_dist_miss)  ///
	mtitle("Baseline without 0 dist" "Adding control for move dist" "<50th percentile move_dist" ">50th percentile move_dist" )

	eststo clear
	



	
	
	
*	
eststo clear 

	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} 								, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} if move_dist!=.				, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist						, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 ${model3} move_dist_imp move_dist_miss	, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat dwintensity4hnew 	${model3} 								, absorb(zip year grade1 id) vce(cluster id zip)  
	*eststo: reghdfe avgfcat cosine	 					   		momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat down60_any_mjrhwy_4 cosinedistmj*  	momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster id zip)  
	eststo: reghdfe avgfcat dwintensity4hnew 	cosinedistmj*  	momedbyschool momblackbyschool mommarriedbyschool percenthispanic frl TeachDegree size stability mover rdcnt_win10_mjrhwy			, absorb(zip year grade1 id) vce(cluster id zip)  

  esttab using "$output/TX_Response_Distance2__$S_DATE.rtf", ///
	compress append b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of alternative measures on outcomes, cluster zip id") keep(down60_any_mjrhwy_4 move_dist dwintensity4hnew move_dist_imp move_dist_miss) ///
	nomtitle
	
eststo clear 

restore


*************************************************************
*Table A6: different clustering
*************************************************************
preserve

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)



*clustered on school
foreach var in down60_any_mjrhwy_4    { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
	
	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster school)  
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id)  
eststo: reghdfe `i' `var' ${model3} , absorb(zip year grade1 id) vce(cluster id school)  

	estadd scalar mean_outcome=`m'


}

  esttab using "$output/TableA6clusteringlevels_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

restore

*************************************************************
*Table A7: cut by different distances
*************************************************************

drop if changein6or9id==0

	keep if (grade1 == 5 | grade1 == 6 | grade1 == 8 | grade1 == 9)

*distance by 0.1 mi bins
foreach var in down60_any_mjrhwy_4    { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
	
	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} if distmj1==1, absorb(zip year grade1 id) vce(cluster id zip)  
eststo: reghdfe `i' `var' ${model3} if distmj2==1, absorb(zip year grade1 id) vce(cluster id zip)  
eststo: reghdfe `i' `var' ${model3} if distmj3==1, absorb(zip year grade1 id) vce(cluster id zip)  
eststo: reghdfe `i' `var' ${model3} if distmj4==1, absorb(zip year grade1 id) vce(cluster id zip)  

	estadd scalar mean_outcome=`m'


}

  esttab using "$output/TableA7cutbydistance1_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}

*distance by 0.2 mi bins
foreach var in down60_any_mjrhwy_4    { 
	foreach i in avgfcat behaveincident rabsent graderep {  //
	
	sum `i' if down60_any_mjrhwy_4~=. & grade1~=. & size~=. & zip~=.
	local m = r(mean)
	
eststo: reghdfe `i' `var' ${model3} if distmj1==1 | distmj2==1, absorb(zip year grade1 id) vce(cluster id zip)  
eststo: reghdfe `i' `var' ${model3} if distmj3==1 | distmj4==1, absorb(zip year grade1 id) vce(cluster id zip)  

	estadd scalar mean_outcome=`m'


}

  esttab using "$output/TableA7cutbydistancehalves_`var'_$S_DATE.rtf", ///
	compress replace b(4) se(4) onecell label nonumbers star(* .1 ** .05 *** .01) ///
	title ("effect of `var' on outcomes") keep(`var') scalars(mean_outcome)
	
eststo clear 
}



log close
